/**
 * Title: LoginController.java
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2019-7-26 17:39
 * @description Project Name: Grote
 * @Package: com.srct.service.account.controller
 */
package com.sharp.authorization.controller.login;

import com.sharp.authorization.annotation.auth.Auth;
import com.sharp.authorization.annotation.auth.LoginAuthRole;
import com.sharp.authorization.annotation.auth.NoneAuthRole;
import com.sharp.authorization.annotation.captcha.CaptchaVerify;
import com.sharp.authorization.annotation.msg.MsgCodeVerify;
import com.sharp.authorization.holder.ClientTypeHolder;
import com.sharp.authorization.service.captcha.CaptchaService;
import com.sharp.authorization.service.login.LoginService;
import com.sharp.authorization.service.sms.MsgVerificationService;
import com.sharp.authorization.service.token.TokenService;
import com.sharp.authorization.vo.login.LoginReq;
import com.sharp.authorization.vo.login.LoginRes;
import com.sharp.authorization.vo.login.MsgLoginReq;
import com.sharp.authorization.vo.login.QrCodeReq;
import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.kernel.constant.err.ErrCodeSys;
import com.sharp.kernel.utils.JsonUtil;
import com.sharp.kernel.validate.Validator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Auth
@Api(value = "登录操作", tags = "用户账户操作-登录")
@RestController("LoginController")
@RequestMapping(value = "")
public class LoginController {

    @Resource
    private TokenService tokenService;
    @Resource
    private LoginService loginService;
    @Resource
    private CaptchaService captchaService;
    @Resource
    private MsgVerificationService msgVerificationService;


    @ApiOperation(value = "账号密码登录", notes = "利用账号密码登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "loginId", value = "用户名或者手机号或者邮箱", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "password", value = "密码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "graphCode", value = "图形验证码", required = true)})
    @CaptchaVerify("LOGIN_CAPTCHA")
    public ResponseEntity<Response<LoginRes>> login(LoginReq loginReq) {
        LoginRes res = loginService.login(loginReq.getLoginId(), loginReq.getPassword());
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "短信验证码登录", notes = "利用短信验证码注册")
    @RequestMapping(value = "/login/msg", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "phoneNumber", value = "手机号码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "loginMsgCode", value = "短信验证码", required = true)})
    @MsgCodeVerify("LOGIN_MSG_CODE")
    public ResponseEntity<Response<LoginRes>> login(MsgLoginReq msgLoginReq) {
        LoginRes res = loginService.loginOrReg(msgLoginReq.getPhoneNumber());
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "通过扫码信息进行登录", notes = "通过扫码信息进行登录")
    @RequestMapping(value = "/login/qrCode", method = RequestMethod.POST)
    public ResponseEntity<Response<LoginRes>> loginByQrCode(String qrCode) {
        QrCodeReq qrCodeReq = JsonUtil.readJson(qrCode, QrCodeReq.class);
        LoginRes res = loginService.loginByQrCode(qrCodeReq);
        return ResponseHandler.generateResponse(res);
    }

    @Auth(NoneAuthRole.class)
    @ApiOperation(value = "用户REFRESH TOKEN登录", notes = "利用refresh token登录")
    @RequestMapping(value = "/login/refresh", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "refreshToken", value = "刷新验证码", required = true)})
    public ResponseEntity<Response<LoginRes>> login(String refreshToken) {
        String clientType = ClientTypeHolder.get();
        String tokenType = tokenService.getTokenType(refreshToken);
        Validator.assertTrue(tokenType.startsWith(clientType), ErrCodeSys.PA_DATA_DIFF, "TOKEN类型");
        LoginRes res = loginService.refreshLogin(refreshToken);
        return ResponseHandler.generateResponse(res);
    }

    @Auth(LoginAuthRole.class)
    @ApiOperation(value = "退出登录", notes = "退出登录")
    @RequestMapping(value = "/logoff", method = RequestMethod.POST)
    public ResponseEntity<Response<String>> logoff() {
        tokenService.removeToken();
        return ResponseHandler.generateResponse("");
    }
}
